package com.prestamomo.vil.ppa.pmvilpage;

import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.prestamomo.vil.ppa.BuildConfig;
import com.prestamomo.vil.ppa.R;
import com.prestamomo.vil.ppa.pmvilapi.PmCheckLoan;
import com.prestamomo.vil.ppa.pmvilapi.PmInitLoan;
import com.prestamomo.vil.ppa.pmvilapi.PmvilCheckPage;
import com.prestamomo.vil.ppa.pmvilapi.PmvilHttpRequest;
import com.prestamomo.vil.ppa.pmvilapi.PmvilLanResult;
import com.prestamomo.vil.ppa.pmvilapi.PmvilSerConfig;
import com.prestamomo.vil.ppa.pmvilapi.PmvilSerllerPack;
import com.prestamomo.vil.ppa.pmvilmedium.pmviluse.PmvilTosMd;
import com.prestamomo.vil.ppa.pmvilmedium.pmvilview.PmvilHorizontalSelector;
import com.prestamomo.vil.ppa.pmvilmedium.pmviluse.PmvilSpMd;
import com.prestamomo.vil.ppa.pmvilnet.PmvilAjustinfse;
import com.prestamomo.vil.ppa.pmvilnet.pmvillistener.PmvilCallBack;
import com.prestamomo.vil.ppa.pmvilpage.pmfitter.PmvilSellerFitter;
import com.prestamomo.vil.ppa.pmvilpage.pmrot.PmvilAtyRoot;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class PmvilSellerVy extends PmvilAtyRoot {

    private ImageView sellerPmvilIvback;
    private PmvilHorizontalSelector sellerPmvilPhsele;
    private TextView sellerPmvilTvmoney;
    private TextView sellerPmvilTvsq;
    private RecyclerView sellerPmvilRecycler;
    private boolean pmvilBlack;
    private int pmvilCheckNum;
    private int pmvilMinCheckNum;
    private PmvilSellerFitter pmvilSellerFitter;
//    private PmvilCheckPage pmvilSeller;
    private List<PmInitLoan.ApplyInfosBean> pmvilapplyInfosList;

    @Override
    protected int getPmvilLayoutId() {
        return R.layout.seller_pmvil_ly;
    }

    @Override
    protected void showPmvilUi() {
        sendAdjPmvilEvent(PmvilAjustinfse.pmvil_krthbdfr);
        sendAdjPmvilEvent(PmvilAjustinfse.pmvil_jtrdr);
        sellerPmvilIvback = findViewById(R.id.seller_pmvil_iv_back);
        sellerPmvilPhsele = findViewById(R.id.seller_pmvil_phsele);
        sellerPmvilTvmoney = findViewById(R.id.seller_pmvil_tv_money);
        sellerPmvilTvsq = findViewById(R.id.seller_pmvil_tv_sq);
        sellerPmvilRecycler = findViewById(R.id.seller_pmvil_recycler);

    }

    @Override
    protected void clickePmvil() {
        sellerPmvilIvback.setOnClickListener(v -> finish());
        sellerPmvilTvsq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_tytrer);
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_gfherhe);
                if (pmvilBlack) {
                    showPmvilLoading();
                    new CountDownTimer(3000, 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                        }

                        @Override
                        public void onFinish() {
                            hindPmvilLoading();
                            showErrSellPmvilSq();
                        }
                    }.start();
                } else {
                    if(pmvilapplyInfosList!=null&&pmvilapplyInfosList.size()!=0){
                        List<String> pmvilCodeProduct = new ArrayList<>();
                        for (int i = 0; i < pmvilapplyInfosList.size(); i++) {
                            if (i < pmvilCheckNum) {
                                pmvilCodeProduct.add(pmvilapplyInfosList.get(i).getProductCode());
                            } else {
                                break;
                            }
                        }
                        PmvilHttpRequest.postApplyPmvilInfo(PmvilSellerVy.this, pmvilCodeProduct, 4, new PmvilCallBack<PmvilLanResult>() {
                            @Override
                            public void onPmvilSucc(String pmvilMethod, PmvilLanResult pmvilContent) {
                                hindPmvilLoading();
                                boolean pmvilIsAllTrue = false;
                                for (PmvilLanResult.OrdersPmvil pmvilorder : pmvilContent.getPmvilorders()) {
                                    sendAdjPmvilEvent(PmvilAjustinfse.pmvil_bdrer);
                                    if (pmvilorder.isPmvilstatus()) {
                                        pmvilIsAllTrue = true;
                                    } else {
                                        pmvilIsAllTrue = false;
                                        break;
                                    }
                                }
                                if (pmvilIsAllTrue) {
                                    pmvilUserId = (String) PmvilSpMd.getPmvaParam(pmvilAy, "pmvil_userId_pm", "");
                                    boolean pmvaUserParam = (boolean) PmvilSpMd.getPmvaParam(pmvilAy, pmvilUserId + "_pmvil", false);
                                    if (pmvaUserParam) {
                                        showRateSellPmvilOrnoDlg(false);
                                    } else {
                                        SimpleDateFormat pmvilSimpleDateFormat = new SimpleDateFormat("yyyy-Mm-dd");
                                        pmvilFormat = pmvilSimpleDateFormat.format(new Date());
                                        boolean pmvilFdtuser = (boolean) PmvilSpMd.getPmvaParam(pmvilAy, pmvilFormat + "_" + pmvilUserId + "_pmvil", false);
                                        if (pmvilFdtuser) {
                                            showRateSellPmvilOrnoDlg(false);
                                        } else {
                                            showRateSellPmvilOrnoDlg(true);
                                        }
                                    }
                                } else {
                                    showErrSellPmvilSq();
                                }
                            }

                            @Override
                            public void onPmvilLogOn(String pmvilMod, PmvilLanResult pmvilCtent) {
                                outpmvilLongin();
                            }

                            @Override
                            public void onPmvilOther(String pmvilMhod, PmvilLanResult pmvilCnt, String pmvilCode, String pmvilMsg) {
                                tostPmvilErrMsg(pmvilMsg);
                            }

                            @Override
                            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                                tostPmvilErrMsg(pmvilError);
                            }
                        });

                    }else{
                        PmvilTosMd.showPmvaMsg("暫無可申請的産品");
                    }
                   }
            }
        });
    }

    @Override
    protected void loadPmvilData() {
        pmvilBlack = getIntent().getBooleanExtra("pmvil_black", false);
//        if (pmvilBlack) {
//            getBlackPmvilSeller();
//        } else {
////            pmvilSeller = (PmvilCheckPage) getIntent().getSerializableExtra("pmvil_seller");
//            setSellerPmvilWhite();
//        }
        setSellerPmvilWhite();

    }

//    private void setSellerPmvilWhite(PmvilCheckPage pmvilSeller) {
    private void setSellerPmvilWhite() {
        loanInit();
       /* if (pmvilSellerList.size() > 0) {
            pmvilSellerList.clear();
        }

        double pmvilMinMy = 0, pmvilMaxMy = 0;
        List<String> pmvilWhiteList = new ArrayList<>();
        pmvilapplyInfosList = pmvilSeller.getPmvilapplyInit().getPmvilapplyInfos();
        for (PmvilCheckPage.ApplyInitPmvil.ApplyInfosPmvil pmvilapplyInfo : pmvilapplyInfosList) {
            pmvilSellerList.add(new PmvilSerllerPack(pmvilapplyInfo.getPmvilproductName(), pmvilapplyInfo.getPmvilproductIcon(), pmvilapplyInfo.getPmvilamountInterval()));
            pmvilMinMy += pmvilapplyInfo.getPmvilminLoanAmount();
            pmvilMaxMy += pmvilapplyInfo.getPmvilmaxLoanAmount();
            pmvilWhiteList.add("$" + pmvilDobleToStr(pmvilMinMy) + "-" + pmvilDobleToStr(pmvilMaxMy));
        }

        if (pmvilSeller.getPmvilapplyInit().getPmvilrecommendNum() == 0) {
            pmvilMinCheckNum = 1;
        } else {
            if (pmvilSeller.getPmvilapplyInit().getPmvilrecommendNum() > pmvilSeller.getPmvilapplyInit().getPmvilapplyInfos().size()) {
                pmvilMinCheckNum = pmvilSeller.getPmvilapplyInit().getPmvilapplyInfos().size();
            } else {
                pmvilMinCheckNum = pmvilSeller.getPmvilapplyInit().getPmvilrecommendNum();
            }
        }
        pmvilCheckNum = pmvilMinCheckNum;
        sellerPmvilTvmoney.setText(pmvilWhiteList.get(pmvilMinCheckNum - 1));
        sellerPmvilPhsele.setPmvilRange(0, pmvilSellerList.size(), pmvilMinCheckNum);
        sellerPmvilPhsele.setPmvilOnSelectChangeListener(new PmvilHorizontalSelector.PmvilOnSelectChangeListener() {
            @Override
            public void onPmvilSelectChange(int pmvilValue) {
                pmvilCheckNum = pmvilValue;
                Log.d("TAG", "selected value: " + pmvilValue);
                sellerPmvilTvmoney.setText(pmvilWhiteList.get(pmvilCheckNum - 1));
                if (pmvilSellerFitter != null) {
                    pmvilSellerFitter.setPmvilListSellers(pmvilCheckNum);
                }
            }
        });
        pmvilSellerFitter = new PmvilSellerFitter(PmvilSellerVy.this, pmvilSellerList, pmvilMinCheckNum);
        sellerPmvilRecycler.setAdapter(pmvilSellerFitter);*/
    }

    private void loanInit() {
        showPmvilLoading();
        pmvilBlack = false;
        PmvilHttpRequest.getInitLoan(this, BuildConfig.VERSION_NAME, 4, new PmvilCallBack<PmInitLoan>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, PmInitLoan pmvilContent) {
                hindPmvilLoading();
                if (pmvilContent.getApplyInfos().size() != 0) {


                    if (pmvilSellerList.size() > 0) {
                        pmvilSellerList.clear();
                    }

                    double pmvilMinMy = 0, pmvilMaxMy = 0,pmxils=0;
                    List<String> pmvilWhiteList = new ArrayList<>();
                    pmvilapplyInfosList = pmvilContent.getApplyInfos();
                    for (PmInitLoan.ApplyInfosBean pmvilapplyInfo : pmvilapplyInfosList) {
                        pmvilSellerList.add(new PmvilSerllerPack(pmvilapplyInfo.getProductName(), pmvilapplyInfo.getProductIcon(),
                                "$"+pmvilapplyInfo.getLoanAmount(),"$"+pmvilapplyInfo.getReviewAmount(),
                                pmvilapplyInfo.getRepayDate(),"$"+pmvilapplyInfo.getServiceCharge(),pmvilapplyInfo.getLowestInterestRate()));
                        pmvilMinMy += pmvilapplyInfo.getMinLoanAmount();
                        pmvilMaxMy += pmvilapplyInfo.getMaxLoanAmount();
                        pmxils += pmvilapplyInfo.getLoanAmount();
//                        pmvilWhiteList.add("$" + pmvilDobleToStr(pmvilMinMy) + "-" + pmvilDobleToStr(pmvilMaxMy));
                        pmvilWhiteList.add("$" + pmvilDobleToStr(pmxils) );
                    }

                    if (pmvilContent.getRecommendNum() == 0) {
                        pmvilMinCheckNum = 1;
                    } else {
                        if (pmvilContent.getRecommendNum() > pmvilContent.getApplyInfos().size()) {
                            pmvilMinCheckNum = pmvilContent.getApplyInfos().size();
                        } else {
                            pmvilMinCheckNum = pmvilContent.getRecommendNum();
                        }
                    }
                    pmvilCheckNum = pmvilMinCheckNum;
                    sellerPmvilTvmoney.setText(pmvilWhiteList.get(pmvilMinCheckNum - 1));
                    sellerPmvilPhsele.setPmvilRange(0, pmvilSellerList.size(), pmvilMinCheckNum);
                    sellerPmvilPhsele.setPmvilOnSelectChangeListener(new PmvilHorizontalSelector.PmvilOnSelectChangeListener() {
                        @Override
                        public void onPmvilSelectChange(int pmvilValue) {
                            pmvilCheckNum = pmvilValue;
                            Log.d("TAG", "selected value: " + pmvilValue);
                            sellerPmvilTvmoney.setText(pmvilWhiteList.get(pmvilCheckNum - 1));
                            if (pmvilSellerFitter != null) {
                                pmvilSellerFitter.setPmvilListSellers(pmvilCheckNum);
                            }
                        }
                    });
                    pmvilSellerFitter = new PmvilSellerFitter(PmvilSellerVy.this, pmvilSellerList, pmvilMinCheckNum);
                    sellerPmvilRecycler.setAdapter(pmvilSellerFitter);
                } else {
//                    pmvilBlack = true;
//                    getBlackPmvilSeller();
                }
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, PmInitLoan pmvilCtent) {
                outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, PmInitLoan pmvilCnt, String pmvilCode, String pmvilMsg) {
                tostPmvilErrMsg(pmvilMsg);
//                pmvilBlack = true;
//                getBlackPmvilSeller();
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
//                pmvilBlack = true;
//                getBlackPmvilSeller();

            }
        });
    }

    List<PmvilSerllerPack> pmvilSellerList = new ArrayList<>();

//    private void getBlackPmvilSeller() {
//        List<String> listXuz = new ArrayList<>();
//        listXuz.add("$1000~5000");
//        listXuz.add("$2000~10000");
//        listXuz.add("$3000~15000");
//        pmvilMinCheckNum = 1;
//        if (pmvilSellerList.size() > 0) {
//            pmvilSellerList.clear();
//        }
//        for (int i = 0; i < 3; i++) {
//            pmvilSellerList.add(new PmvilSerllerPack("產品名稱", "https://kwwld.ldwdl", "$1000~5000"));
//        }
//        sellerPmvilTvmoney.setText(listXuz.get(pmvilMinCheckNum - 1));
//        sellerPmvilPhsele.setPmvilRange(0, pmvilSellerList.size(), pmvilMinCheckNum);
//        sellerPmvilPhsele.setPmvilOnSelectChangeListener(new PmvilHorizontalSelector.PmvilOnSelectChangeListener() {
//            @Override
//            public void onPmvilSelectChange(int pmvilValue) {
//                pmvilCheckNum = pmvilValue;
//                Log.d("TAG", "selected value: " + pmvilValue);
//                sellerPmvilTvmoney.setText(listXuz.get(pmvilCheckNum - 1));
//                if (pmvilSellerFitter != null) {
//                    pmvilSellerFitter.setPmvilListSellers(pmvilCheckNum);
//                }
//            }
//        });
//        pmvilSellerFitter = new PmvilSellerFitter(PmvilSellerVy.this, pmvilSellerList, pmvilMinCheckNum);
//        sellerPmvilRecycler.setAdapter(pmvilSellerFitter);
////        showPmvilLoading();
//        /*PmvilHttpRequest.getPmvilConfig(this, "ListaProdukteve", new PmvilCallBack<PmvilSerConfig>() {
//            @Override
//            public void onPmvilSucc(String pmvilMethod, PmvilSerConfig pmvilContent) {
//                hindPmvilLoading();
//                pmvilMinCheckNum = 1;
//                List<PmvilSerConfig.ExtInfoArrPmvil.BlackPmvilSeller> pmvilalc = pmvilContent.getPmvilextInfoArr().get(0).getPmvilalc();
//                Collections.shuffle(pmvilalc);
//                if (pmvilSellerList.size() > 0) {
//                    pmvilSellerList.clear();
//                }
//                for (int i = 0; i < pmvilalc.size(); i++) {
//                    if (i > 2) {
//                        break;
//                    } else {
//                        pmvilSellerList.add(new PmvilSerllerPack(pmvilalc.get(i).getPmvilnimi(), pmvilalc.get(i).getPmvilaadress(), pmvilalc.get(i).getPmvilvahemik()));
//                    }
//                }
//                sellerPmvilTvmoney.setText(pmvilContent.getPmvilextInfoArr().get(0).getPmvilvalik().get(pmvilMinCheckNum - 1));
//                sellerPmvilPhsele.setPmvilRange(0, pmvilSellerList.size(), pmvilMinCheckNum);
//                sellerPmvilPhsele.setPmvilOnSelectChangeListener(new PmvilHorizontalSelector.PmvilOnSelectChangeListener() {
//                    @Override
//                    public void onPmvilSelectChange(int pmvilValue) {
//                        pmvilCheckNum = pmvilValue;
//                        Log.d("TAG", "selected value: " + pmvilValue);
//                        sellerPmvilTvmoney.setText(pmvilContent.getPmvilextInfoArr().get(0).getPmvilvalik().get(pmvilCheckNum - 1));
//                        if (pmvilSellerFitter != null) {
//                            pmvilSellerFitter.setPmvilListSellers(pmvilCheckNum);
//                        }
//                    }
//                });
//                pmvilSellerFitter = new PmvilSellerFitter(PmvilSellerVy.this, pmvilSellerList, pmvilMinCheckNum);
//                sellerPmvilRecycler.setAdapter(pmvilSellerFitter);
//
//            }
//
//            @Override
//            public void onPmvilLogOn(String pmvilMod, PmvilSerConfig pmvilCtent) {
//                outpmvilLongin();
//            }
//
//            @Override
//            public void onPmvilOther(String pmvilMhod, PmvilSerConfig pmvilCnt, String pmvilCode, String pmvilMsg) {
//                tostPmvilErrMsg(pmvilMsg);
//            }
//
//            @Override
//            public void onPmvilFailure(String pmvilMod, String pmvilError) {
//                tostPmvilErrMsg(pmvilError);
//            }
//        });*/
//    }

    private void showErrSellPmvilSq() {
        AlertDialog.Builder errPmvilBlder = new AlertDialog.Builder(this, R.style.pm_dlg_stl);
        View pmvilErrView = this.getLayoutInflater().inflate(R.layout.err_pmvil_sq_dlg, null);
        errPmvilBlder.setView(pmvilErrView);
        errPmvilBlder.setCancelable(false);
        AlertDialog pmvilErrDlg = errPmvilBlder.create();
        pmvilErrDlg.show();
        ImageView errPmvileImClose = pmvilErrView.findViewById(R.id.err_pmvil_sqdlg_iv_close);
        TextView errPmvilTvOk = pmvilErrView.findViewById(R.id.err_pmvil_sqdlg_tv_ok);
        errPmvileImClose.setOnClickListener(v -> pmvilErrDlg.dismiss());
        errPmvilTvOk.setOnClickListener(v -> {
            pmvilErrDlg.dismiss();
        });
        pmvilErrDlg.setOnDismissListener(dialog -> check());
    }


    /*private void getCheckSellPmvilPage() {
        showPmvilLoading();
        PmvilHttpRequest.getCheckPmvilPage(this, new PmvilCallBack<PmvilCheckPage>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, PmvilCheckPage pmvilContent) {

                if (pmvilContent.getPmvilauthType() != null) {
                    hindPmvilLoading();
                    if (pmvilContent.getPmvilauthType().equals("pmvil_user")) {
                        startActivity(new Intent(pmvilAy, PmvilSourceVy.class));
                        finish();
                    } else if (pmvilContent.getPmvilauthType().equals("pmvil_ht") || pmvilContent.getPmvilauthType().equals("pmvil_ocr")) {
                        Intent pmvilIntent = new Intent(pmvilAy, PmvilPlaceVy.class);
                        if (pmvilContent.getPmvilauthType().equals("pmvil_ht")) {
                            pmvilIntent.putExtra("pmvil_isface", true);
                        } else {
                            pmvilIntent.putExtra("pmvil_isface", false);
                        }
                        pmvilIntent.putExtra("pmvil_ischange", pmvilContent.isPmvilmodifyCertificate());
                        startActivity(pmvilIntent);
                        finish();
                    } else if (pmvilContent.getPmvilauthType().equals("pmvil_bank")) {
                        startActivity(new Intent(pmvilAy, PmvilBlockVy.class));
                        finish();
                    }
                } else {
                    switch (pmvilContent.getPmvilpageNum()) {
                        case -2:
                            pmvilBlack = true;
                            getBlackPmvilSeller();
                            break;
                        case -1:
                            hindPmvilLoading();
                            startActivity(new Intent(pmvilAy, PmvilCensorVy.class));
                            finish();
                            break;
                        case 1:
                            hindPmvilLoading();
                            sendAdjPmvilEvent(PmvilAjustinfse.pmvil_gdrgetj);
                            sendAdjPmvilEvent(PmvilAjustinfse.pmvil_fdgrger);
                            Intent pmvilSellerIntent = new Intent(pmvilAy, PmvilListingVy.class);
                            pmvilSellerIntent.putExtra("pmvil_seller", pmvilContent);
                            startActivity(pmvilSellerIntent);
                            finish();
                            break;
                        case 4:
                            hindPmvilLoading();
                            pmvilBlack = false;
//                            pmvilSeller = pmvilContent;
//                            setSellerPmvilWhite(pmvilSeller);
                            break;
                    }
                }
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, PmvilCheckPage pmvilCtent) {
                outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, PmvilCheckPage pmvilCnt, String pmvilCode, String pmvilMsg) {
                tostPmvilErrMsg(pmvilMsg);
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                tostPmvilErrMsg(pmvilError);
            }
        });
    }*/

    private String pmvilDobleToStr(double pmvilNum) {
        NumberFormat numberPmvilInstance = NumberFormat.getNumberInstance();
        numberPmvilInstance.setGroupingUsed(true);
        return numberPmvilInstance.format(pmvilNum);
    }

    String pmvilUserId = "";
    String pmvilFormat = "";
    boolean isPmvilFinish = false;

    public void showRateSellPmvilOrnoDlg(boolean isPmvilPj) {
        if (isPmvilPj) {
            SimpleDateFormat pmvilSimpleDateFormat = new SimpleDateFormat("yyyy-Mm-dd");
            pmvilFormat = pmvilSimpleDateFormat.format(new Date());
            pmvilUserId = (String) PmvilSpMd.getPmvaParam(pmvilAy, "pmvil_userId_pm", "");
        }
        AlertDialog.Builder ratePmvilBlder = new AlertDialog.Builder(this, R.style.pm_dlg_stl);
        View pmvilRateView = this.getLayoutInflater().inflate(R.layout.rate_pmvil_orno_dlg, null);
        ratePmvilBlder.setView(pmvilRateView);
        ratePmvilBlder.setCancelable(false);
        AlertDialog pmvilRateDlg = ratePmvilBlder.create();
        pmvilRateDlg.show();
        ImageView ratePmvileImClose = pmvilRateView.findViewById(R.id.rate_pmvil_orno_ivclose);
        TextView ratePmvilTvms = pmvilRateView.findViewById(R.id.rate_pmvil_orno_tvms);
        LinearLayout ratePmvilIspj = pmvilRateView.findViewById(R.id.rate_pmvil_orno_ispj);
        TextView ratePmvilTvpl = pmvilRateView.findViewById(R.id.wl_pmvildlg_tv_pl);
        TextView ratePmvilTvgoogle = pmvilRateView.findViewById(R.id.wl_pmvildlg_tv_google);

        LinearLayout ratePmvilnopl = pmvilRateView.findViewById(R.id.rate_pmvil_orno_nopl);
        TextView ratePmvilTvqd = pmvilRateView.findViewById(R.id.rate_pmvil_orno_qd);

        if (isPmvilPj) {
            ratePmvilIspj.setVisibility(View.VISIBLE);
            ratePmvilTvms.setVisibility(View.VISIBLE);
            ratePmvilnopl.setVisibility(View.GONE);
        } else {
            ratePmvilTvms.setVisibility(View.GONE);
            ratePmvilIspj.setVisibility(View.GONE);
            ratePmvilnopl.setVisibility(View.VISIBLE);
        }
        ratePmvileImClose.setOnClickListener(v -> {
            pmvilRateDlg.dismiss();
            isPmvilFinish = false;
        });
        ratePmvilTvpl.setOnClickListener(v -> {
            pmvilRateDlg.dismiss();
            isPmvilFinish = true;
            PmvilSpMd.putPmvaParam(pmvilAy, pmvilUserId + "_pmvil", true);
            PmvilSpMd.putPmvaParam(pmvilAy, pmvilFormat + "_" + pmvilUserId + "_pmvil", true);
            startActivity(new Intent(pmvilAy, PmvilAdviseVy.class));
            finish();
        });
        ratePmvilTvgoogle.setOnClickListener(v -> {
            pmvilRateDlg.dismiss();
            isPmvilFinish = true;
            PmvilSpMd.putPmvaParam(pmvilAy, pmvilUserId + "_pmvil", true);
            PmvilSpMd.putPmvaParam(pmvilAy, pmvilFormat + "_" + pmvilUserId + "_pmvil", true);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if (intent.resolveActivity(pmvilAy.getPackageManager()) != null) {
                intent.setData(Uri.parse("market://details?id=" + BuildConfig.APPLICATION_ID));
                startActivity(intent);
            } else {
                intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID));
                if (intent.resolveActivity(pmvilAy.getPackageManager()) != null) { //有浏览器
                    startActivity(intent);
                }
            }
            finish();
        });
        ratePmvilTvqd.setOnClickListener(v -> {
            pmvilRateDlg.dismiss();
            isPmvilFinish = false;
        });

        pmvilRateDlg.setOnDismissListener(dialog -> {
                    if (!isPmvilFinish) {
                        check();
                    }
                }
        );
    }

    //check检查借款
    private void check(){
        showPmvilLoading();
        PmvilHttpRequest.LoanCheck(this,  new PmvilCallBack<PmCheckLoan>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, PmCheckLoan pmvilContent) {
                if(!pmvilContent.getProduct()){
                    hindPmvilLoading();
                    startActivity(new Intent( PmvilSellerVy.this,PmvilCensorVy.class));
                    finish();
                }
                if (pmvilContent.getOnBlackCache()){
                    hindPmvilLoading();
//                    pmvilBlack =true;
//                    getBlackPmvilSeller();
                    return;
                }
                if(!pmvilContent.getOnOrder()){
                    hindPmvilLoading();
                    loanInit();
                    return;
                }
                if(pmvilContent.getOnUser()){
                    hindPmvilLoading();
                    //一键多申
                    loanInit();
                    return;
                }
                loanInitList();
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, PmCheckLoan pmvilCtent) {
                outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, PmCheckLoan pmvilCnt, String pmvilCode, String pmvilMsg) {
//                pmvilBlack =true;
//                getBlackPmvilSeller();
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                tostPmvilErrMsg(pmvilError);
            }
        });
    }

    private void loanInitList() {
        showPmvilLoading();
        PmvilHttpRequest.getInitLoan(this, BuildConfig.VERSION_NAME, 1, new PmvilCallBack<PmInitLoan>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, PmInitLoan pmvilContent) {
                hindPmvilLoading();
                startActivity(new Intent(PmvilSellerVy.this,PmvilListingVy.class));
                finish();
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, PmInitLoan pmvilCtent) {
                outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, PmInitLoan pmvilCnt, String pmvilCode, String pmvilMsg) {
                tostPmvilErrMsg(pmvilMsg);
//               pmvilBlack =true;
//               getBlackPmvilSeller();
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
//                pmvilBlack =true;
//                getBlackPmvilSeller();
            }
        });
    }
}
